package net.caixiaomi.info.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.UiThread;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.WebViewActivity;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.ShareModel;
import net.caixiaomi.info.ui.about.AboutActivity;
import net.caixiaomi.info.ui.dialog.ShareDialogFragment;
import net.caixiaomi.info.ui.football.PaymentActivity;
import net.caixiaomi.info.ui.login.LoginActivity;
import net.caixiaomi.info.ui.login.RegistActivity;
import net.caixiaomi.info.ui.mine.RechargeActivity;
import net.caixiaomi.info.util.SchemeUtil;
import net.caixiaomi.info.util.ToastUtil;

import org.json.JSONException;

/**
 * Created by suntian on 2018/8/30.
 */

public class WebviewPresenter {

    private Context mContext;
    private WebView mWebView;

    public WebviewPresenter(Context context, WebView webview,WebCallBack callback){
        mContext=context;
        mWebView=webview;
        this.callback=callback;
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new AndroidtoJs(), "test");
    }

    // 继承自Object类
    public class AndroidtoJs extends Object {

        // 定义JS需要调用的方法
        // 被JS调用的方法必须加入@JavascriptInterface注解
        @JavascriptInterface
        public void appNative(String msg) {
            try {
                if(TextUtils.isEmpty(msg)){
                    return;
                }
                final org.json.JSONObject obj=new org.json.JSONObject(msg);
                String methodName=obj.getString("methodName");
                switch (methodName){
                    case "channel":
                        mWebView.post(new Runnable() {

                            @Override
                            public void run() {
                                String channel= AppHelper.getChanel(mContext);
//                                channel="c25022";
//                                channel="c27022";
//                                channel="c22024";
                                mWebView.loadUrl("javascript:getChannel('" + channel + "')");
                            }
                        });
                        break;
                    case "login":
                        Intent login = new Intent(mContext, LoginActivity.class);
                        mContext.startActivity(login);
                        break;
                    case "register":
                        Intent register = new Intent(mContext, RegistActivity.class);
                        mContext.startActivity(register);
                        break;
                    case "showTitle":
                        final String title=obj.getString("title");
                        mWebView.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.hideTitle(false);
                                callback.changedTitle(title);
                            }
                        });

                        break;
                    case "hideTitle":
                        mWebView.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.hideTitle(true);
                            }
                        });

                        break;
                    case "hideDelete":
                        mWebView.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.hideDelete(true);
                            }
                        });
                        break;
                    case "getToken":
                        mWebView.post(new Runnable() {
                            @Override
                            public void run() {
                                JSONObject object = new JSONObject();
                                object.put("token", CommonApp.getToken());
                                mWebView.loadUrl("javascript:actionMessage('" + object + "')");
                            }
                        });

                        break;
                    case "pushPayment":
                        String payToken=obj.getString("payToken");
                        Intent payIntent = new Intent(mContext, PaymentActivity.class);
                        payIntent.putExtra("payToken", payToken);
                        mContext.startActivity(payIntent);
                        break;
                    case "pushRechange":
                        try {
                            String price=obj.getString("price");
                            JSONObject priceObj=new JSONObject();
                            priceObj.put("price",price);
                            Intent rechargeIntent = new Intent(mContext, RechargeActivity.class);
                            rechargeIntent.putExtra("data",priceObj.toString());
                            rechargeIntent.putExtra("isDisabled",obj.getString("isDisabled"));
                            rechargeIntent.putExtra("toastPrice",obj.getString("toastPrice"));
                            mContext.startActivity(rechargeIntent);
                        }catch (Exception e){

                        }

                        break;
                    case "goShare":
                        mWebView.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    callback.goShare(obj.getString("title"),obj.getString("description"),obj.getString("url"),obj.getString("thumbUrl"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        break;
                    case "pushUrl":
                        String url=obj.getString("url");
                        SchemeUtil.parseInWebViewUrl(mContext,url,null);
                        break;
                    case "closeWeb":
                        Activity act= (Activity) mContext;
                        act.finish();
                        break;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public void goBack(){
        mWebView.loadUrl("javascript:actionKeyBack()");
    }

    public WebCallBack callback;

    public interface WebCallBack{
        public void changedTitle(String title);
        public void hideTitle(boolean hide);
        public void hideDelete(boolean hide);
        public void goShare(String title,String description,String url,String thumbUrl);
    }
}
